﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 站台
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class StationController : BaseController
    {
        private readonly IStationService stationService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="stationService"></param>
        public StationController(IWebHostEnvironment environment, IStationService stationService) : base(environment)
        {
            this.stationService = stationService;
        }
        /// <summary>
        /// 添加站台信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddStation(Station obj)
        {
            if (string.IsNullOrEmpty(obj.Code) || obj.Code.Length <= 0)
                return Ok(BaseResult.Result("400", "编码不能为空！"));

            obj.CreateUserId = base.BaseGetNowUserId();
            obj.CreateUserName = base.BaseGetNowUserName();
            string message = "";
            bool IsOK = stationService.AddStation(obj, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }
        /// <summary>
        /// 显示站台信息
        /// </summary>
        /// <param name="cname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStationList(string? cname = "", int kid = 0, int pageIndex = 1, int pageSize = 3)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = stationService.GetStationList(cname, kid, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 出库时显示出库站台
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingOrderDetailStation(int type)
        {
            if (type <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = stationService.GetShippingOrderDetailStation(type);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 删除站台信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelStation(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = stationService.DelStation(id);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 批量删除站台信息
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelStationList(long[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            ApiResult apiresult = new ApiResult();
            foreach (var uid in idList)
            {
                bool IsOK = stationService.DelStation(uid);
                if (!IsOK)
                {
                    daRes = false;
                    break;
                }
            }

            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 修改站台信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdStation(Station obj)
        {
            if (obj.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(obj.Code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            obj.ModifyUserId = base.BaseGetNowUserId();
            obj.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            var res = stationService.UpdStation(obj, out message);
            if (res)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(true, "修改失败！" + message, "500"));
        }
        /// <summary>
        /// 根据id获取站台信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStationById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = stationService.GetStationById(id);
            if (res != null)
                return Ok(BaseResult.Result(res, "查询成功", "200"));
            return Ok(BaseResult.Result(null, "查询失败", "500"));
        }

        /// <summary>
        /// 获取站台功能类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmStationFunctionType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmStationFunctionType>();

            return Ok(list);
        }

        /// <summary>
        /// 获取枚举站台类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmStationType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmStationType>();

            return Ok(list);
        }
    } 
}
